﻿using System;
using System.Text;
using System.Web;

namespace InfoJetSoft.Service.Util
{
	
	class ScriptHtmlUtil
	{

		public static string formatHtml( string html )
		{
			html = html.Replace( "\\", "\\\\" );
			html = html.Replace( "\r\n", "\\n" );
			html = html.Replace( "\n", "\\n" );
			html = html.Replace( "'", "\\'" );
			return html;
		}
		
		public static string formatMessage(string message)
		{
			//MessageBacklash.xsn和TestSoapException.xsn
			//根据这两个测试需要添加下句,将\转换为\\.
			//否则在MessageBacklash.xsn中报告Untermitated string constant错误.
			//TestSoapException.xsn中将不能显示SoapException中的\字符.
			message = message.Replace( "\\", "\\\\" ); 
			int beforeN = 0;
			StringBuilder newBuffer = new StringBuilder();
			StringBuilder modalBuffer = new StringBuilder(message);
			for (int m = 0; m < modalBuffer.Length; m++)
			{
				char c = modalBuffer[m];
				if (c == 0xA)
				{
					string line = modalBuffer.ToString(beforeN, m - beforeN);
					newBuffer.Append(line);
					newBuffer.Append("\\n");
					beforeN = m + 1;
				}
				if (c == 0xD) {
					string line = modalBuffer.ToString(beforeN, m - beforeN);
					newBuffer.Append(line);
					newBuffer.Append("\\r");
					beforeN = m + 1;
				}
			}
			if (beforeN < modalBuffer.Length)
			{
				newBuffer.Append(modalBuffer.ToString(beforeN, modalBuffer.Length - beforeN));
			}
			newBuffer = newBuffer.Replace( "'", "\\'" );
			return newBuffer.ToString();
		}

		public static string formatText(string text){
			if( text == null ){
				return "";
			}
			return text.Replace( "\r\n", "<BR>" );
		}

		public static string formatWebContext( HttpContext httpContext, string webContext ){
			if (webContext == null) {
				webContext = "";
			}
			webContext = webContext.Trim();
			if (webContext.EndsWith("/") || webContext.EndsWith("\\")) {
				//ok
			}
			else {
				if (webContext.Length > 0) {
					webContext += "/";
				}
			}
            //CID 0732 //2010.12.19 //Begin
            if (webContext.StartsWith("~"))
            {
                webContext = webContext.Replace("~", PathUtil.getAppPath(httpContext));
            }
            //CID 0732 //2010.12.19 //End
			return webContext;
		}

	}
}